package com.cari.oa.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.CommentEntity;

@Repository
@SuppressWarnings("unchecked")
public class CommentDao extends BaseDao<CommentEntity> {
	
	/**
     * 根据项目id获取项目留言列表
     * @param enname 系统用户英文名
     * @return 系统实体对象
     */
    public List<CommentEntity> getListByObjID(String id, Integer type) {
        Query query = getSession().createQuery("from CommentEntity where objectId = :objectId and objectType = :objectType order by createTime desc");
        query.setString("objectId", id);
        query.setInteger("objectType", type);
        return (List<CommentEntity>)query.list();
    }
    
    /**
     * 获取最新留言时间及其相关项目id
     * @return
     * guotingting 2013-1-24 下午3:13:26
     */
    public List< Map<String,Date>> getMaxDateList(Integer type, Set<String> objectIds) {
//    	StringBuilder str = new StringBuilder();
//    	str.append("SELECT max(id) as id, object_id, max(object_type) as object_type, max(user_enname) as user_enname, max(user_cnname) as user_cnname, max(content) as content,");
//    	str.append("max(create_time) as create_time from t_comment ");
//    	if(type != null) {
//    		str.append("where object_type = ");
//    		str.append( type );
//        }
//    	str.append(" group by object_id");
//        Query query = getSession().createSQLQuery(str.toString()).addEntity(CommentEntity.class);
    	StringBuilder str = new StringBuilder();
    	str.append("select new Map(c.objectId as objectId, max(c.createTime) as createTime) from CommentEntity c where 1=1 ");
    	if(type != null) str.append("and c.objectType = :objectType ");
    	if(objectIds.size() > 0) str.append("and c.objectId in (:objectIds) ");
    	str.append("group by c.objectId");
    	Query query = getSession().createQuery(str.toString());
    	if(type != null) query.setInteger("objectType", type);
    	if(objectIds.size() > 0)query.setParameterList("objectIds", objectIds);
    	return (List< Map<String, Date>>) query.list();
    }
    
    /**
	 * 查询同一id所对应的留言记录
	 * @author -- Heyyroup
	 * @2013-1-17 下午2:31:20
	 * @param costId
	 * @return
	 */
	public Long getCountByObjId(String Id){
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("select count(*) from CommentEntity u where 1=1 and u.objectId like ");
		stringBuilder.append("'"+Id+"'");
		
		Query query = getSession().createQuery(stringBuilder.toString());
        return (Long)query.uniqueResult();
	}

}
